<template>
    <van-nav-bar
        title="编辑收货地址"
        left-text="返回"
        left-arrow
        @click-left="onBack"
    />

    <van-address-edit
        :area-list="areaList"
        show-set-default
        :address-info="info"
        show-delete
        :area-columns-placeholder="['请选择', '请选择', '请选择']"
        @save="onSave"
        @delete="onDel"
    />
</template>

<script setup>
import { areaList } from '@vant/area-data';
import { getCurrentInstance, reactive, onMounted, ref } from 'vue';

const { proxy } = getCurrentInstance();
const business = reactive(proxy.$cookies.get('business') ?? {});
const addrid = ref(proxy.$route.query.addrid);
const info = reactive({});

onMounted(() => {
    getAddressInfo();
});

const getAddressInfo = async () => {
    let data = {
        busid:business.id,
        addrid:addrid.value
    }

    let result = await proxy.$Api.addressInfo(data);

    if(result && result.code === 0)
    {
        proxy.$showNotify({
            type:'warning',
            message:result.msg,
            duration:2000,
            onClose:() => {
                proxy.$router.back();
            }
        });

        return;
    }

    let Info = result.data;

    Object.assign(info,{
        name:Info.consignee,
        tel:Info.mobile,
        areaCode:Info.district ? Info.district : (Info.city ? Info.city : Info.province),
        addressDetail:Info.address,
        isDefault:Info.status == 1 ? true : false
    });

}

const onSave = async (values) => {
    let data = {
        id:addrid.value,
        busid:business.id,
        consignee:values.name,
        address:values.addressDetail,
        mobile:values.tel,
        code:values.areaCode,
        status:values.isDefault === true ? 1 : 0
    }

    let result = await proxy.$Api.addressEdit(data);

    if(result && result.code === 1)
    {
        proxy.$showNotify({
            type:'success',
            message:result.msg,
            duration:2000,
            onClose:() => {
                proxy.$router.back();
            }
        });

        return;
    }else{
        proxy.$showNotify({
            type:'warning',
            message:result.msg,
            duration:2000
        });

        return;
    }

}

const onDel = (info) => {
    proxy.$showConfirmDialog({
        title:'删除收货地址',
        message:'确认删除该收货地址？'
    }).then(async () => {
        let data = {
            busid:business.id,
            addrid:addrid.value
        }

        let result = await proxy.$Api.addressDel(data);

        if(result && result.code === 1)
        {
            proxy.$showNotify({
                type:'success',
                message:result.msg,
                duration:2000,
                onClose:() => {
                    proxy.$router.back();
                }
            });

            return;
        }else{
            proxy.$showNotify({
                type:'warning',
                message:result.msg,
                duration:2000
            });

            return;
        }

    }).catch((err) => {
        
    });
 
}

const onBack = () => {
    proxy.$router.back();
}
</script>